package com.ruoyi.offer.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

import com.ruoyi.bigbase.api.RemoteCustomerService;
import com.ruoyi.bigbase.api.domain.IntegralDetails;
import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.merchant.api.RemoteMerchantService;
import com.ruoyi.merchant.api.domain.Merchant;
import com.ruoyi.merchant.api.domain.MerchantVerification;
import com.ruoyi.offer.mapper.TotalAmountMapper;
import com.ruoyi.offer.util.QRCodeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.offer.mapper.OrdersMapper;
import com.ruoyi.offer.domain.Orders;
import com.ruoyi.offer.service.IOrdersService;

/**
 * 订单表Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-05-10
 */
@Service
public class OrdersServiceImpl implements IOrdersService 
{
    @Autowired
    private OrdersMapper ordersMapper;
    @Autowired
    private RemoteMerchantService remoteMerchantService;
    @Autowired
    private TotalAmountMapper totalAmountMapper;

    @Autowired
    private RemoteCustomerService remoteCustomerService;

    /**
     * 查询订单表
     * 
     * @param id 订单表主键
     * @return 订单表
     */
    @Override
    public Orders selectOrdersById(Long id)
    {

        return ordersMapper.selectOrdersById(id);
    }

    /**
     * 查询订单表列表
     * 
     * @param orders 订单表
     * @return 订单表
     */
    @Override
    public List<Orders> selectOrdersList(Orders orders)
    {

        return ordersMapper.selectOrdersList(orders);
    }

    /**
     * 新增订单表
     * 
     * @param orders 订单表
     * @return 结果
     */
    @Override
    public int insertOrders(Orders orders)
    {
        // 生成订单编号
        generateOrderNumber(orders);
        // 生成核销码
        String verificationCode = QRCodeUtil.generateVerificationCode();
        orders.setVerificationCode(verificationCode);
        // 生成核销二维码
        String verificationImg = QRCodeUtil.generateQRCodeImage(verificationCode);
        orders.setVerificationImg(verificationImg);
return ordersMapper.insertOrders(orders);
    }

    /**
     * 生成订单编号
     * @param orders 订单对象
     */
    private void generateOrderNumber(Orders orders) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String format = sdf.format(new Date());
        String randomPart = String.format("%04d", new Random().nextInt(9999));
        String orderNum = "ORD" + format + randomPart;
        orders.setOrderNum(orderNum);
    }
    /**
     * 修改订单表
     * 
     * @param orders 订单表
     * @return 结果
     */
    @Override
    public int updateOrders(Orders orders)
    {
//        List<MerchantVerification> getlist = remoteMerchantService.getlist(null,SecurityConstants.INNER);
//        System.out.println(getlist);

        System.out.println(orders);
        IntegralDetails integralDetails = new IntegralDetails();
        integralDetails.setUserId(Math.toIntExact(orders.getUserId()));
        System.out.println(integralDetails);
        if (orders.getOrderStatus()==1){
            integralDetails.setContent("支付订单");
            integralDetails.setScore(2);
            totalAmountMapper.updateByMid(Math.toIntExact(orders.getMerchantId()),totalAmountMapper.selectByMid(Math.toIntExact(orders.getMerchantId())).add(orders.getOrderPrice()));
        }else if (orders.getOrderStatus()==2){
            integralDetails.setContent("订单使用");
            integralDetails.setScore(3);
        }else if (orders.getOrderStatus()==3){
            integralDetails.setContent("评价成功");
            integralDetails.setScore(1);
        }

        remoteCustomerService.addPoints(integralDetails,SecurityConstants.INNER);

        return ordersMapper.updateOrders(orders);
    }

    /**
     * 批量删除订单表
     * 
     * @param ids 需要删除的订单表主键
     * @return 结果
     */
    @Override
    public int deleteOrdersByIds(Long[] ids)
    {
        return ordersMapper.deleteOrdersByIds(ids);
    }

    /**
     * 删除订单表信息
     * 
     * @param id 订单表主键
     * @return 结果
     */
    @Override
    public int deleteOrdersById(Long id)
    {
        return ordersMapper.deleteOrdersById(id);
    }

    @Override
    public int selectOrdersCount(Long merchantId) {
        int num = ordersMapper.selectOrdersCount(merchantId);
        return num;
    }
}
